package com.reservoir.auth.controller;

import com.reservoir.auth.form.AuthBody;
import com.reservoir.auth.service.AuthService;
import com.reservoir.core.entity.RestMsg;
import com.reservoir.core.exception.auth.HasLoginException;
import com.reservoir.log.annotation.Log;
import com.reservoir.log.enums.BusinessType;
import com.reservoir.log.enums.OperatorType;
import com.reservoir.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author STARS
 * @create 2021-11-02
 */
@RestController
@Api(tags = "授权接口")
public class AuthController {
    @Autowired
    private AuthService authService;
    @Autowired
    private RedisService redisService;


    @PostMapping("/login")
    @ApiOperation("登录授权")
    @Log(title = "auth",businessType = BusinessType.GRANT,operatorType = OperatorType.OTHER)
    public Object login(@RequestBody AuthBody authBody,
                         @RequestHeader(value = "authorization",required = false) String token) {
        if (redisService.hasKey(token)) {
            throw new HasLoginException();
        }
        return authService.login(authBody);
    }

    @PostMapping("/refresh")
    @ApiOperation("刷新权限")
    public void refresh(@RequestHeader("authorization") String token) {
        authService.refresh(token);
    }

    @DeleteMapping("/logout")
    @ApiOperation("退出账号")
    @Log(title = "auth",businessType = BusinessType.DELETE,operatorType = OperatorType.OTHER)
    public RestMsg<Object> logout(@RequestHeader("authorization") String token) {
        return authService.logout(token);
    }

}
